//
//  LLPriorityQueue.swift
//  LLTryFightingApp
//
//  Created by jinbang.li on 2024/9/27.
//

import Foundation
/**
 优先队列
 */
struct LLPriorityQueue<E,P:Comparable> {
    ///E为元素 P为优先级
    private var elements:[(E,P)] = []
    // struct mutating 关键字修改属性的值
    mutating func enqueue(_ element:E,withP p:P) {
        elements.append((element,p))
        elements.sort { $0.1 > $1.1 }
    }
    
    mutating func dequeue() -> E? {
        return elements.isEmpty ? nil : elements.removeLast().0
    }
    
    func peek() -> E? {
        return elements.first?.0
    }
}
